package com.conero.yangsu.controller.system;

import com.conero.yangsu.entity.AdminEntity;
import com.conero.yangsu.entity.FieldEntity;
import com.conero.yangsu.entity.ModuleEntity;
import com.conero.yangsu.lib.FeedbackData;
import com.conero.yangsu.mapper.AdminMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;

@RestController
@RequestMapping("/system/admin")
public class AdminController {
    @Autowired
    private AdminMapper adminMapper;

    @GetMapping("/list")
    public FeedbackData list(){
        List<AdminEntity> list = (List<AdminEntity>) adminMapper.findAll();
        HashMap<String, Object> result = new HashMap<>();
        result.put("list", list);
        return FeedbackData.success(result, "请求成功");
    }

    // 数据列获取
    @PostMapping("/getList")
    public FeedbackData getList(@RequestParam(value = "current", defaultValue = "1") Integer current) {
        Page<AdminEntity> model = adminMapper.findAll(PageRequest.of(current - 1, 20));
        HashMap<String, Object> result = new HashMap<>();
        List<AdminEntity> dataList = model.getContent();
        result.put("data", dataList);

        HashMap<String, Object> pagination = new HashMap<>();
        pagination.put("total", model.getNumber());
        pagination.put("current", current);
        result.put("pagination", pagination);
        return FeedbackData.success(result, "数据请求成功！");
    }

    //数据新增
    @PostMapping("/add")
    public FeedbackData add(AdminEntity adminEntity) {
        adminMapper.save(adminEntity);
        return FeedbackData.success("数据新增成功！");
    }

    //数据新增
    @PostMapping("/upd")
    public FeedbackData upd(AdminEntity adminEntity) {
        adminMapper.save(adminEntity);
        return FeedbackData.success("数据更新成功！");
    }

    //数据新增
    @PostMapping("/del")
    public FeedbackData del(AdminEntity adminEntity) {
        if (adminEntity.getId() == null) {
            return FeedbackData.error("数据删除失败，缺少必要参数！");
        }
        adminMapper.deleteById(adminEntity.getId());
        return FeedbackData.success("数据更新成功！");
    }
}
